package com.jeesite.modules.tk.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.em.PushConten;
import com.jeesite.modules.pt.entity.PtPush;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.tk.entity.TkTask;
import com.jeesite.modules.tk.dao.TkTaskDao;
import org.springframework.web.client.RestTemplate;

/**
 * tk_taskService
 * @author 李寒冰
 * @version 2024-12-05
 */
@Service
public class TkTaskService extends CrudService<TkTaskDao, TkTask> {

	@Autowired
	RestTemplate restTemplate ;

	@Value(value = "${pic.reqUrl}")
	private String reqUrl;
	
	/**
	 * 获取单条数据
	 * @param tkTask
	 * @return
	 */
	@Override
	public TkTask get(TkTask tkTask) {
		return super.get(tkTask);
	}
	
	/**
	 * 查询分页数据
	 * @param tkTask 查询条件
	 * @param tkTask page 分页对象
	 * @return
	 */
	@Override
	public Page<TkTask> findPage(TkTask tkTask) {
		return super.findPage(tkTask);
	}
	
	/**
	 * 查询列表数据
	 * @param tkTask
	 * @return
	 */
	@Override
	public List<TkTask> findList(TkTask tkTask) {
		return super.findList(tkTask);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param tkTask
	 */
	@Override
	@Transactional
	public void save(TkTask tkTask) {
		super.save(tkTask);
		String taskStatus = tkTask.getTaskStatus();
		//任务状态：0 审核中 1已上线  2已下架 3审核失败 4违规 5结束
		if(taskStatus.equals("1") || taskStatus.equals("3")){
			PtPush ptPush = new PtPush();
			if(taskStatus.equals("1")){
				PushConten.copyToPush(PushConten.PTTASKTONGGUO,ptPush);
			}else if(taskStatus.equals("2")){
				PushConten.copyToPush(PushConten.TASKXIAJIA,ptPush);
			}else{
				PushConten.copyToPush(PushConten.PTTASKBOHUI,ptPush);
			}
			Map<String,Object> map = new HashMap<>();
			map.put("taskId",tkTask.getId());
			ptPush.setUserId(tkTask.getUserId());
			Map<String,Object> payload = new HashMap<>();
			payload.put("id",tkTask.getId());
			payload.put("taskName",tkTask.getTitle());
			ptPush.setPayload(payload);
			JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(ptPush));
			HttpHeaders headers = new HttpHeaders();
			headers.setContentType(MediaType.APPLICATION_JSON);
			HttpEntity<String> jsonEntity = new HttpEntity<String>(jsonObject.toJSONString(), headers);
			try{
				String result = restTemplate.postForObject(reqUrl+"/api/push/pushMsg",jsonEntity,String.class);
			}catch (Exception e){
				logger.error("推送异常：",e);
			}
		}
	}

	/**
	 * 更新状态
	 * @param tkTask
	 */
	@Transactional
	public void savePtTask(TkTask tkTask) {
		if(StringUtils.isEmpty(tkTask.getTaskCode())){
			String forObject = restTemplate.getForObject(reqUrl + "/api/util/getCodeTask", String.class);
			JSONObject jsonObject = JSONObject.parseObject(forObject);
			tkTask.setTaskCode(jsonObject.getString("data"));
		}
		super.save(tkTask);
	}
	
	/**
	 * 更新状态
	 * @param tkTask
	 */
	@Override
	@Transactional
	public void updateStatus(TkTask tkTask) {
		super.updateStatus(tkTask);
	}
	
	/**
	 * 删除数据
	 * @param tkTask
	 */
	@Override
	@Transactional
	public void delete(TkTask tkTask) {
		super.delete(tkTask);
	}
	
}